package cn.wgx.module.ent.service;

import cn.wgx.common.base.IBaseService;
import cn.wgx.common.entity.ent.DTOEntInvite;
import cn.wgx.common.entity.ent.EntInvite;
import cn.wgx.common.entity.ent.EntUser;
import cn.wgx.common.entity.ent.Enterprise;
import cn.wgx.common.entity.sys.SysUser;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.module.ent.mapper.EntInviteMapper;
import cn.wgx.module.sys.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EntInviteService extends IBaseService<EntInviteMapper, EntInvite> {

    @Autowired
    EntUserService entUserService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    EntUserAppService entUserAppService;
    @Autowired
    EntAppService entAppService;
    @Autowired
    UserService userService;

    //包装列表
    public List<DTOEntInvite> invite(Page page, QueryWrapper<EntInvite> queryWrapper) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        if (loginAppUser == null) {
            return null;
        }
        queryWrapper.eq("user_id", loginAppUser.getUser_id());
        IPage<EntInvite> e = getBaseMapper().selectPage(page, queryWrapper);
        List<DTOEntInvite> dtoEntInvites = new ArrayList<>();
        for (EntInvite entInvite : e.getRecords()) {
            dtoEntInvites.add(wrap(entInvite));
        }
        return dtoEntInvites;
    }
    public List<DTOEntInvite> iInvite(Page page, QueryWrapper<EntInvite> queryWrapper) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        if (loginAppUser == null) {
            return null;
        }
        queryWrapper.eq("createUserId", loginAppUser.getUser_id());
        IPage<EntInvite> e = getBaseMapper().selectPage(page, queryWrapper);
        List<DTOEntInvite> dtoEntInvites = new ArrayList<>();
        for (EntInvite entInvite : e.getRecords()) {
            dtoEntInvites.add(wrap(entInvite));
        }
        return dtoEntInvites;
    }
    //包装通用
    public DTOEntInvite wrap(EntInvite entInvite) {
        if (null == entInvite) {
            return null;
        }
        DTOEntInvite dTOEntInvite = new DTOEntInvite();
        BeanUtils.copyProperties(entInvite, dTOEntInvite);
        if (entInvite.getUser_id() != null && entInvite.getEnt_id() != null) {
            Enterprise ent = enterpriseService.getById(entInvite.getEnt_id());
            if (ent != null) {
                dTOEntInvite.setEntname(ent.getName());
            }
            SysUser sysUser = userService.getById(entInvite.getUser_id());
            if (sysUser != null) {
                dTOEntInvite.setNickname(sysUser.getNickname());
            }
            SysUser sysUser1 = userService.getById(entInvite.getCreateUserId());
            if (sysUser1 != null) {
                dTOEntInvite.setInvitename(sysUser1.getNickname());
            }
        }
        return dTOEntInvite;
    }

    public int accept(Integer invite_id) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        EntInvite entInvite = new EntInvite();
        entInvite.setInvite_id(invite_id);
        entInvite.setStatus(1);
        entInvite.setAcceptDate(new Date());
        entInvite.setUpdateUserId(loginAppUser.getUser_id());
        int i = baseMapper.updateById(entInvite);
        if (i > 0) {
            //用户接受邀请后加入企业（进入ent_user表）
            EntInvite byId = getById(invite_id);
            String entCode = enterpriseService.getById(byId.getEnt_id()).getEntCode();
            EntUser entUser = new EntUser();
            entUser.setUser_id(loginAppUser.getUser_id());
            entUser.setEntCode(entCode);
            entUser.setEnt_id(byId.getEnt_id());
            entUserService.save(entUser);
        }
        return i;
    }

    public int reject(Integer invite_id) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        EntInvite entInvite = new EntInvite();
        entInvite.setInvite_id(invite_id);
        entInvite.setStatus(2);
        entInvite.setAcceptDate(new Date());
        entInvite.setUpdateUserId(loginAppUser.getUser_id());
        return baseMapper.updateById(entInvite);
    }
}
